package com.sqbang.dbcompare.controller;

import com.sqbang.dbcompare.config.PathConfig;
import com.sqbang.dbcompare.pojo.dto.DbInfoRequest;
import com.sqbang.dbcompare.pojo.vo.R;
import com.sqbang.dbcompare.util.CompareDatabaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

/**
 * 主要的控制器
 * @author suqiongbang
 * @date 2020/12/28 10:33
 */
@RestController
public class MainController {

    @Autowired
    PathConfig pathConfig;

    @GetMapping("/test")
    public R<Boolean> test() {
        return R.ok(true);
    }

    @PostMapping("/create")
    public R<String> create(@RequestBody DbInfoRequest request) {
        try {
            String templatePath = System.getProperty("user.dir") + File.separator + pathConfig.getTemplateDiskDir() + File.separator + pathConfig.getTemplateFileName();
            String reportPath = System.getProperty("user.dir") + File.separator + pathConfig.getTemplateDiskDir() + File.separator + pathConfig.getReportFileName();
            CompareDatabaseUtil.getInstance().create(request, templatePath, reportPath);
            return R.ok(pathConfig.getTemplateDiskDir() + "/" + pathConfig.getReportFileName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error("生成失败");
    }
}
